package prob4;

import com.sun.javafx.collections.MappingChange;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.stage.FileChooser;

import javax.xml.bind.JAXB;
import java.io.File;
import java.io.FileNotFoundException;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class Controller {

    @FXML
    private Button btn;

    @FXML
    private TextArea textarea;

    @FXML
    void btnpressed(ActionEvent event) {
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("Open .txt File");
        fileChooser.setInitialDirectory(new File("./src/prob4"));
        fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("txt文件", "*.txt"));
        File file=fileChooser.showOpenDialog(btn.getParent().getScene().getWindow());
        TreeSet<String> treeSet=new TreeSet<String>(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });
        try(Scanner scanner=new Scanner(file)) {
            while (scanner.hasNextLine())
            {
                String[] strings=scanner.nextLine().split("\\p{P}|[ ]");
                treeSet.addAll(Arrays.asList(strings).stream().filter(v->!v.equals("")).collect(Collectors.toList()));
                treeSet.stream().forEach((value)->{textarea.appendText(value+" ");});
                textarea.appendText("\n");
            }
        } catch (FileNotFoundException e) {

        }
    }
}
